<?php
define('ROOT_DIR', $_SERVER['DOCUMENT_ROOT'].'/');
$folderPath = ROOT_DIR .'uploads/canvas';
if (!is_dir($folderPath)) {
    http_response_code(404);
    echo json_encode(['error' => '文件夹不存在']);
    exit;
}
$files = scandir($folderPath);
$files = array_filter($files, function ($file) {
    return $file !== '.' && $file !== '..';
});
$fileDetails = array_map(function ($file) use ($folderPath) {
    $filePath = $folderPath . '/' . $file;
    $type = is_dir($filePath) ? 'directory' : pathinfo($filePath, PATHINFO_EXTENSION);
    return [
        'name' => $file,
        'type' => $type
    ];
}, $files);
header('Content-Type: application/json');
echo json_encode($fileDetails);
?>